<?php

namespace App\Http\Controllers\Api;

use App\Models\NewMenu as Menu;
//use Encore\Admin\Form;
//use Encore\Admin\Layout\Column;
//use App\Admin\Layout\Content;
//use Encore\Admin\Layout\Row;
//use Encore\Admin\Tree;
//use App\Admin\Tree;
//use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class MenuController extends \App\Http\Controllers\Controller
{
//    use HasResourceActions;

    protected $title = '菜单';

    public function index()
    {

//        debug_sql();
        $menu = new Menu();
        $tree =  $menu->toTree();


        return [
            'status'=> 1,
            'dat'=> $tree,
        ];
    }


    public function store(Request $req)
    {
        $all = $req->all();
        if (! isset($all['title'])) {
            return $this->resError('菜单信息不全');
        }
        $obj = new Menu($all);
        if ($obj->save()) {
            return $this->resSuccess();
        }

        return $this->resError();
    }

    public function update($id, Request $req)
    {
        $all = $req->all();
        if (! isset($all['title'])) {
            return $this->resError('菜单信息不全');
        }
        $obj = Menu::find($id);
        if (!$obj) {
            return $this->resError('菜单信息有误！');
        }
        $obj->fill($all);
        if ($obj->save()) {
            return $this->resSuccess();
        }

        return $this->resError();
    }


    protected function treeView()
    {
//        $menuModel = config('admin.database.menu_model');
        $menuModel = NewMenu::class;
        $tree = new Tree(new $menuModel());

        $tree->branch(function ($branch) {
            $payload = "&nbsp; <strong>{$branch['title']}</strong>";
            if (!isset($branch['children'])) {
                if (url()->isValidUrl($branch['uri'])) {
                    $uri = $branch['uri'];
                } else {
                    $uri = admin_url($branch['uri']);
                }
                $payload .= "&nbsp;&nbsp;&nbsp;<a href=\"$uri\" class=\"dd-nodrag\">$uri</a>";
            }
            return $payload;
        });
//        $tree->disableButton(['edit']);

        return $tree;
    }

    /**
     * Edit interface.
     *
     * @param string  $id
     * @param Content $content
     *
     * @return Content
     */
   /* public function edit($id, Content $content)
    {
        return $content
            ->title(trans('admin.menu'))
            ->description(trans('admin.edit'))
            ->row($this->form()->edit($id));
    }*/



    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {

        $menuModel = NewMenu::class;
        $form = new Form(new $menuModel());

        $form->display('id', 'ID');
        $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());
        $form->text('title', trans('admin.title'))->rules('required');
        $form->text('uri', trans('admin.uri'));
        $form->text('order', '排序')->help('越小越靠前');
        $form->switch('active', '是否启用');


        $form->display('created_at', trans('admin.created_at'));
        $form->display('updated_at', trans('admin.updated_at'));
        $form->disableViewCheck();
        $form->disableCreatingCheck();

        $form->tools(function($tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }

    /**
     * Help message for icon field.
     *
     * @return string
     */
    protected function iconHelp()
    {
        return 'For more icons please see <a href="http://fontawesome.io/icons/" target="_blank">http://fontawesome.io/icons/</a>';
    }



}
